package com.hzcx.app.simplechat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.hyphenate.chat.EMClient;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.util.badge.BadgeUtil;

/**
 * 聊天界面
 */
public class ChatActivity extends BaseActivity {

    public static final String INTENT_USER_ID = "INTENT_USER_ID";
    public static final String INTENT_USER_NICK_NAME = "INTENT_USER_NICK_NAME";
    public static final String INTENT_USER_REMARK_NAME = "INTENT_USER_REMARK_NAME";
    public static final String INTENT_USER_HEAD = "INTENT_USER_HEAD";

    public static final String INTENT_HX_USER_INFO = "INTENT_HX_USER_INFO";

    private ChatFragment chatFragment;

    @Override
    public int getLayout() {
        return R.layout.activity_chat;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

        // TODO: 2020/8/27 为了避免聊天界面重复使用，这里使用fragment填充界面
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        chatFragment = new ChatFragment();
        chatFragment.setArguments(getIntent().getExtras());
        transaction.add(R.id.frame_layout, chatFragment);
        transaction.commit();
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        runOnUiThread(() -> {
            int num = EMClient.getInstance().chatManager().getUnreadMessageCount();
            if (num > 0) {
                BadgeUtil.setBadgeCount(this, num, R.mipmap.ic_launcher);
            } else {
                BadgeUtil.resetBadgeCount(this, R.mipmap.ic_launcher);
            }
        });
    }


    public final static int REQUEST_CODE = 1;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == REQUEST_CODE) {
            if (resultCode == ChatGroupSettingActivity.RESULT_CODE) {
                finish();
            }
//        }
    }
}
